package com.ruoyi.eCommerce.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.config.CrawqueueConfig;
import com.ruoyi.system.domain.Shopeeidlistinginfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.eCommerce.domain.Shopeesglistinginfo;
import com.ruoyi.eCommerce.service.IShopeesglistinginfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.RedisUtils;
/**
 * 新加坡shopee商品信息Controller
 * 
 * @author ruoyi
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/shopee/sgshopeelistinginfo")
public class ShopeesglistinginfoController extends BaseController
{
    private String prefix = "shopee/sgshopeelistinginfo";

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private IShopeesglistinginfoService shopeesglistinginfoService;



    @RequiresPermissions("shopee:sgshopeelistinginfo:view")
    @GetMapping()
    public String sgshopeelistinginfo()
    {
        return prefix + "/sgshopeelistinginfo";
    }

    /**
     * 查询新加坡shopee商品信息列表
     */
    @RequiresPermissions("shopee:sgshopeelistinginfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Shopeesglistinginfo shopeesglistinginfo)
    {
        redisUtils.set(CrawqueueConfig.getShopeeStorekey(),"hello");
        startPage();
        List<Shopeesglistinginfo> list = shopeesglistinginfoService.selectShopeesglistinginfoList(shopeesglistinginfo);
        return getDataTable(list);
    }

    /**
     * 导出新加坡shopee商品信息列表
     */
    @RequiresPermissions("shopee:sgshopeelistinginfo:export")
    @Log(title = "新加坡shopee商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Shopeesglistinginfo shopeesglistinginfo)
    {
        List<Shopeesglistinginfo> list = shopeesglistinginfoService.selectShopeesglistinginfoList(shopeesglistinginfo);
        ExcelUtil<Shopeesglistinginfo> util = new ExcelUtil<Shopeesglistinginfo>(Shopeesglistinginfo.class);
        return util.exportExcel(list, "sgshopeelistinginfo");
    }

    /**
     * 新增新加坡shopee商品信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新加坡shopee商品信息
     */
    @RequiresPermissions("shopee:sgshopeelistinginfo:add")
    @Log(title = "新加坡shopee商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Shopeesglistinginfo shopeesglistinginfo)
    {
        return toAjax(shopeesglistinginfoService.insertShopeesglistinginfo(shopeesglistinginfo));
    }
    /**
     * 图片展示
     */
    @GetMapping("/showimage/{itemid}")
    public String showimage(@PathVariable("itemid") String itemid, ModelMap mmap)
    {
        Shopeesglistinginfo shopeesglistinginfo = shopeesglistinginfoService.selectShopeesglistinginfoById(itemid);
        mmap.put("shopeelistinginfo", shopeesglistinginfo);
        String[] imagesNames = shopeesglistinginfo.getImages().replaceAll("'","").replaceAll("\\[","").replaceAll("\\]","").split(",");
        List<String> images = new ArrayList<>();
        for(String  imagename: imagesNames){
            String rooturl="https://cf.shopee.com.my/file/";
            String imageurl = rooturl+imagename.trim();
            images.add(imageurl);
        }
        mmap.put("shopeelistinginfo", shopeesglistinginfo);
        mmap.put("images", images);
        return prefix + "/showimage";
    }
    /**
     * 修改新加坡shopee商品信息
     */
    @GetMapping("/edit/{itemid}")
    public String edit(@PathVariable("itemid") String itemid, ModelMap mmap)
    {
        Shopeesglistinginfo shopeesglistinginfo = shopeesglistinginfoService.selectShopeesglistinginfoById(itemid);
        mmap.put("shopeesglistinginfo", shopeesglistinginfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存新加坡shopee商品信息
     */
    @RequiresPermissions("shopee:sgshopeelistinginfo:edit")
    @Log(title = "新加坡shopee商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Shopeesglistinginfo shopeesglistinginfo)
    {
        return toAjax(shopeesglistinginfoService.updateShopeesglistinginfo(shopeesglistinginfo));
    }

    /**
     * 删除新加坡shopee商品信息
     */
    @RequiresPermissions("shopee:sgshopeelistinginfo:remove")
    @Log(title = "新加坡shopee商品信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(shopeesglistinginfoService.deleteShopeesglistinginfoByIds(ids));
    }
}
